package com.wswy.wzcx.model;

import android.os.Parcel;
import android.text.TextUtils;
import com.wswy.wzcx.utils.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CarInfoModel implements android.os.Parcelable {

  public String carId;

  /**
   * 车牌号
   */
  public String carNo;

  /**
   * 发动机号
   */
  public String engineNo;

  /**
   * 车架号
   */
  public String frameNo;

  /**
   * 车系
   */
  public String series;
  public String seriesId;

  /**
   * 车型
   */
  public String model;
  public String specificationId;

  public List<OCRItem> ocrResult;

  private String noWithoutProvince = null;
  private String province = null;

  public String carType;

  public String ocrHash;

  public String getNoWithoutProvince(){

    if (noWithoutProvince == null && !TextUtils.isEmpty(carNo)){
      StringBuilder sb =new StringBuilder();
      int length = carNo.length();
      for (int i = 0; i < length; i++) {
        char c = carNo.charAt(i);
        if (StringUtils.isLetterOrDigit(c)){
          sb.append(c);
        }
      }
      noWithoutProvince = sb.toString().toUpperCase();
    }
    return noWithoutProvince;
  }

  public String getProvince(){
    if (province == null && !TextUtils.isEmpty(carNo)){
      String s = carNo.substring(0,1);
      if (StringUtils.isProvince(s)){
        province = s;
      }
    }
    return province;
  }

  public CarInfoModel(){

  }


  private boolean isFromOcr(){
    return ocrHash !=null && TextUtils.equals(ocrHash,toOcrHash());
  }

  String toOcrHash(){
    return carNo+engineNo+frameNo;
  }

  public Map<String,String> convertQueryMap(){

    Map<String, String> map = new HashMap<>();
    map.put("carNo",carNo);
    map.put("frameNo",frameNo);
    map.put("engineNo",engineNo);
    map.put("seriesId",seriesId);
    map.put("specificationId",specificationId);
    map.put("carType",carType);
    if (isFromOcr()){
      map.put("fromOCR","1");
    }
    return map;
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.carId);
    dest.writeString(this.carNo);
    dest.writeString(this.engineNo);
    dest.writeString(this.frameNo);
    dest.writeString(this.series);
    dest.writeString(this.seriesId);
    dest.writeString(this.model);
    dest.writeString(this.specificationId);
    dest.writeTypedList(this.ocrResult);
    dest.writeString(this.carType);
    dest.writeString(this.ocrHash);
  }

  protected CarInfoModel(Parcel in) {
    this.carId = in.readString();
    this.carNo = in.readString();
    this.engineNo = in.readString();
    this.frameNo = in.readString();
    this.series = in.readString();
    this.seriesId = in.readString();
    this.model = in.readString();
    this.specificationId = in.readString();
    this.ocrResult = in.createTypedArrayList(OCRItem.CREATOR);
    this.carType = in.readString();
    this.ocrHash = in.readString();
  }

  public static final Creator<CarInfoModel> CREATOR = new Creator<CarInfoModel>() {
    @Override
    public CarInfoModel createFromParcel(Parcel source) {
      return new CarInfoModel(source);
    }

    @Override
    public CarInfoModel[] newArray(int size) {
      return new CarInfoModel[size];
    }
  };
}
